package com.mlnx.data.service.iml;

import com.mlnx.common.utils.MyLog;
import com.mlnx.data.entity.BpInfo;
import com.mlnx.data.enums.OblCodeEnum;
import com.mlnx.data.pojo.form.KangTaiForm;
import com.mlnx.data.pojo.form.OblBpInfoForm;
import com.mlnx.data.pojo.form.UrenBpInfoForm;
import com.mlnx.data.pojo.form.ZhengKangBpInfoForm;
import com.mlnx.data.pojo.vo.BpKangTaiVo;
import com.mlnx.data.pojo.vo.OblResponseVo;
import com.mlnx.data.pojo.vo.ZhengKangBpResultVo;
import com.mlnx.data.service.WaiBuBpService;
import com.mlnx.device.entity.Device;
import com.mlnx.device.service.DeviceService;
import com.mlnx.stream.service.DeviceBroadcastService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by amanda.shan on 2019/11/1.
 */
@Service
public class WaiBuBpServiceIml implements WaiBuBpService {

    private MyLog logger = MyLog.getLog(getClass());

    @Autowired
    private DeviceBroadcastService deviceBroadcastService;
    @Autowired
    private DeviceService deviceService;

    @Override
    public BpKangTaiVo saveKangTaiBp(KangTaiForm kangTaiForm) {

        if (kangTaiForm != null) {
            logger.info("==================================\n接收到康泰3G 血压信息:" + kangTaiForm.toString());
        }

        BpKangTaiVo bpKangTaiResult = new BpKangTaiVo();
        bpKangTaiResult.setCode(BpKangTaiVo.OK);

        if (kangTaiForm.getDeviceSim() == null || kangTaiForm.getHighPressure() == null ||
                kangTaiForm.getLowVoltage() == null || kangTaiForm.getPulse() == null ||
                kangTaiForm.getUploadTime() == null) {
            bpKangTaiResult.setCode(BpKangTaiVo.RE_SEND);
            bpKangTaiResult.setMsg("参数缺失");
        } else {
            try {

                SimpleDateFormat kangTaiBpTimeFormat = new SimpleDateFormat("yyyyMMddHHmmss");

                Date date = kangTaiBpTimeFormat.parse(kangTaiForm.getUploadTime());
                logger.info("接收康泰血压时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

                BpInfo bpInfo = new BpInfo();
                bpInfo.setDeviceId(kangTaiForm.getDeviceSim());
                bpInfo.setSbp(kangTaiForm.getHighPressure());
                bpInfo.setDbp(kangTaiForm.getLowVoltage());
                bpInfo.setHeart(kangTaiForm.getPulse());
                bpInfo.setPacketTime(System.currentTimeMillis());
                bpInfo.setTime(date.getTime());

                deviceBroadcastService.castBpInfo(bpInfo);

            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                e.printStackTrace();
                bpKangTaiResult.setCode(BpKangTaiVo.RE_SEND);
                bpKangTaiResult.setMsg("数据异常");
            }
        }

        logger.info("==================================\n");

        if (bpKangTaiResult.getCode() == BpKangTaiVo.OK) {
            bpKangTaiResult.setStatus(BpKangTaiVo.OK_STATUS);
        }
        return bpKangTaiResult;
    }

    @Override
    public OblResponseVo saveOrbBpDeviceInfo(OblBpInfoForm orbBpInfo) {
        SimpleDateFormat orbResponseFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat orbBpDeviceFormat = new SimpleDateFormat("yyyyMMddHHmmss");

        OblResponseVo orbResponse = new OblResponseVo(OblCodeEnum.SUCCESS.getCode(), orbResponseFormat.format(new Date()));

        if (orbBpInfo != null) {
            logger.info("==================================\n接收到欧宝莱3G 血压信息:" + orbBpInfo.toString());
        }

        if (StringUtils.isEmpty(orbBpInfo.getTime())) {
            orbResponse.setStatusCode(OblCodeEnum.TIME_EMPTY.getCode());
        } else if (StringUtils.isEmpty(orbBpInfo.getImei())) {
            orbResponse.setStatusCode(OblCodeEnum.IMEI_EMPTY.getCode());
        } else if (orbBpInfo.getSys() == null) {
            orbResponse.setStatusCode(OblCodeEnum.SBP_EMPTY.getCode());
        } else if (orbBpInfo.getDia() == null) {
            orbResponse.setStatusCode(OblCodeEnum.DBP_EMPTY.getCode());
        } else if (orbBpInfo.getPul() == null) {
            orbResponse.setStatusCode(OblCodeEnum.HEART_EMPTY.getCode());
        } else if (StringUtils.isEmpty(orbBpInfo.getUser())) {
            orbResponse.setStatusCode(OblCodeEnum.USER_EMPTY.getCode());
        } else {
            try {
                orbBpInfo.setTime(orbBpInfo.getTime().replaceAll("\"", ""));
                Date date = orbBpDeviceFormat.parse(orbBpInfo.getTime());
                logger.info("接收到欧宝莱3G 血压时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

                BpInfo bpInfo = new BpInfo();
                bpInfo.setDeviceId(orbBpInfo.getImei().replaceAll("\"", ""));
                bpInfo.setSbp(Integer.valueOf(orbBpInfo.getSys()));
                bpInfo.setDbp(Integer.valueOf(orbBpInfo.getDia()));
                bpInfo.setHeart(Integer.valueOf(orbBpInfo.getPul()));
                bpInfo.setPacketTime(System.currentTimeMillis());
                bpInfo.setTime(date.getTime());

                deviceBroadcastService.castBpInfo(bpInfo);

            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                e.printStackTrace();
            }
        }
        logger.info("==================================\n");

        if (!orbResponse.getStatusCode().equals(OblCodeEnum.SUCCESS.getCode())) {
            logger.info("欧宝莱血压数据错误码：{}", OblCodeEnum.getMsg(orbResponse.getStatusCode()));
        }

        return orbResponse;
    }

    @Override
    public String saveUrenBpDeviceInfo(UrenBpInfoForm urenBpInfoForm) {
        // OK201412220930
        SimpleDateFormat urenResponseFormat = new SimpleDateFormat("yyyyMMddHHmm");
        SimpleDateFormat urenBpDeviceFormat = new SimpleDateFormat("yyyy-MM-dd/HH:mm:ss");


        if (urenBpInfoForm != null) {
            logger.info("==================================\n接收到优瑞恩3G 血压信息:" + urenBpInfoForm.toString());
        }

        if (StringUtils.isEmpty(urenBpInfoForm.getTime())) {
            logger.error("设备血压时间为空");
        } else {
            try {
                urenBpInfoForm.setTime(urenBpInfoForm.getTime().replaceAll("\"", ""));
                Date date = urenBpDeviceFormat.parse(urenBpInfoForm.getTime());
                logger.info("接收到优瑞嗯3G 血压时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

                BpInfo bpInfo = new BpInfo();
                bpInfo.setDeviceId(urenBpInfoForm.getImei().replaceAll("\"", ""));
                bpInfo.setSbp(Integer.valueOf(urenBpInfoForm.getSys()));
                bpInfo.setDbp(Integer.valueOf(urenBpInfoForm.getDia()));
                bpInfo.setHeart(Integer.valueOf(urenBpInfoForm.getPul()));
                bpInfo.setPacketTime(System.currentTimeMillis());
                bpInfo.setTime(date.getTime());

                deviceBroadcastService.castBpInfo(bpInfo);

            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                e.printStackTrace();
            }
        }
        logger.info("==================================\n");

        String result = "OK" + urenResponseFormat.format(new Date());
        return result;
    }

    @Override
    public ZhengKangBpResultVo saveZhengKangBp3GDeviceInfo(ZhengKangBpInfoForm deviceBpInfo) {
        // 正康
        SimpleDateFormat zhengKangeResultFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat zhengKangeBpTimeFormat = new SimpleDateFormat("yyyy-M-d/H:m:s");

        if (deviceBpInfo != null) {
            logger.info("==================================\n接收到正康3G 血压信息:" + deviceBpInfo.toString());
        }

        ZhengKangBpResultVo bpZhengKangResult = new ZhengKangBpResultVo();
        bpZhengKangResult.setRescode(ZhengKangBpResultVo.OK);
        bpZhengKangResult.setDatetime(zhengKangeResultFormat.format(new Date()));

        if (deviceBpInfo.getUser() == null) {
            bpZhengKangResult.setRescode(ZhengKangBpResultVo.ERROR);
        } else if (deviceBpInfo.getUser() != 0) {
            if (StringUtils.isEmpty(deviceBpInfo.getTime())) {
                logger.error("设备血压时间为空");
                bpZhengKangResult.setRescode(ZhengKangBpResultVo.ERROR);
            } else {
                try {
                    deviceBpInfo.setTime(deviceBpInfo.getTime().replaceAll("\"", ""));
                    Date date = zhengKangeBpTimeFormat.parse(deviceBpInfo.getTime());
                    logger.info("接收正康血压时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

                    BpInfo bpInfo = new BpInfo();
                    bpInfo.setDeviceId(deviceBpInfo.getImei().replaceAll("\"", ""));
                    bpInfo.setSbp(Integer.valueOf(deviceBpInfo.getSys()));
                    bpInfo.setDbp(Integer.valueOf(deviceBpInfo.getDia()));
                    bpInfo.setHeart(Integer.valueOf(deviceBpInfo.getPul()));
                    bpInfo.setPacketTime(System.currentTimeMillis());
                    bpInfo.setTime(date.getTime());
                    Device device=deviceService.getDevice(bpInfo.getDeviceId());
                    if(device!=null){
                        bpInfo.setPatientId(device.getPatientId());
                    }
                    deviceBroadcastService.castBpInfo(bpInfo);

                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                    e.printStackTrace();
                    bpZhengKangResult.setRescode(ZhengKangBpResultVo.ERROR);
                }
            }
        }

        logger.info("==================================\n");
        return bpZhengKangResult;
    }
}
